/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if (!head)return head;
        ListNode* pp1, * pp2;
        ListNode* p1 = new ListNode(0);
        ListNode* p2 = new ListNode(0);
        pp1 = p1;
        pp2 = p2;
        while (head) {
            if (head->val < x) {
                p1->next = head;
                p1 = p1->next;
            }
            else {
                p2->next = head;
                p2 = p2->next;
            }
            head = head->next;
        }
        p2->next = nullptr;
        p1->next = pp2->next;
        return pp1->next;
    }
};